import 'package:enjoy_life/pages/home/index.dart';
import 'package:enjoy_life/pages/mine/index.dart';
import 'package:enjoy_life/utils/EventsBus.dart';
import 'package:flutter/material.dart';

// ignore: must_be_immutable
class NavTabBar extends StatefulWidget {
  const NavTabBar({super.key});

  @override
  State<NavTabBar> createState() => _NavTabBarState();
}

class _NavTabBarState extends State<NavTabBar> {
  // 当前高亮索引
  int _currentIndex = 0;
  // 底部TabBar数据
  List tabBarList = [
    {
      "title": "首页",
      "default": "assets/tabs/home_default.png",
      "active": "assets/tabs/home_active.png"
    },
    {
      "title": "我的",
      "default": "assets/tabs/my_default.png",
      "active": "assets/tabs/my_active.png"
    }
  ];

  @override
  void initState() {
    super.initState();

    eventBus.on<AuthEvent>().listen((event) {
      // print('跳转至登录页');
      // 跳转至登录页
      Navigator.pushNamed(context, '/login');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: _currentIndex,
        children: [
          // 首页页面
          HomePage(),
          // 我的页面
          MinePage(
            currentIndex: _currentIndex,
          ),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          // 当前高亮索引
          currentIndex: _currentIndex,
          // 点击事件
          onTap: (value) {
            setState(() {
              // 更新索引navBar索引
              _currentIndex = value;
            });
          },
          items: tabBarList.map((item) {
            return BottomNavigationBarItem(
                icon: Image.asset(
                  item["default"],
                  width: 25,
                  height: 25,
                ),
                activeIcon: Image.asset(
                  item["active"],
                  width: 25,
                  height: 25,
                ),
                label: item["title"]);
          }).toList()),
    );
  }
}
